package ${cfg.basePackage}.module.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import ${cfg.basePackage}.common.project.SRS;
import ${cfg.basePackage}.common.project.base.BaseDao;
import ${cfg.basePackage}.mybatis.gen.dao.${entity}Dao;
import ${cfg.basePackage}.mybatis.gen.entity.${entity};
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @date ${date}
 */
@Slf4j
@Service
public class ${entity}SerImpl implements ${entity}Ser {
    @Autowired
    private ${entity}Dao ${table.entityPath}Dao;
    @Autowired
    private BaseDao baseDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SRS insert(${entity} ${table.entityPath}) {
        //               查询实体
        //         ${entity} ${table.entityPath} = ${table.entityPath}Dao.selectOne(wrapper().eq("id", "3"));
        //
        //               查询自定义
        //         SRS ${table.entityPath} = baseDao.selectOne(${entity}.TABLE_NAME, "*", null);
        //
        //                添加实体
        //${table.entityPath}.setId(1L);
        ${table.entityPath}.setId(nextId());
        ${table.entityPath}Dao.insert(${table.entityPath});
        //
        //               添加自定义实体
        //         entity.setId(nextId());
        //         SRS srs = SRS.toUnderSRS(${table.entityPath});
        //         insertFill(srs);  // 注入默认数据
        //         System.out.println(srs.toJSONString());
        //         baseDao.insertOne(${entity}.TABLE_NAME, srs);
        //
        //                判断
        //         if (insert != 1) {
        //             exception(ERROR);
        //         }
        //
        //                 抛出异常（回滚）
        //         exception(ERROR);
        return SRS.bySuccess();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SRS delete(Long[] ids) {
        int i = ${table.entityPath}Dao.deleteBatchIds(Arrays.asList(ids));
        //        if (i != ids.size()) {
        //            exception(ERROR);
        //        }
        //         exception(ERROR);
        return SRS.bySuccess();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SRS update(${entity} ${table.entityPath}) {
        //QueryWrapper<${entity}> eq = wrapper().eq(${entity}.ID, 1);
        ${table.entityPath}Dao.updateById(${table.entityPath});
        //        int update = baseDao.update(${entity}.TABLE_NAME, SRS.toUnderSRS(${table.entityPath}), eq);
        //        if (update != 1) {
        //            exception(ERROR);
        //        }
        return SRS.bySuccess();
    }

    @Override
    public SRS list(${entity} ${table.entityPath}) {
        List<${entity}> ${table.entityPath}s1 = ${table.entityPath}Dao.selectList(null);
        //        ArrayList< SRS > ${table.entityPath}s2 = baseDao.selectList(${entity}.TABLE_NAME, "*", null);
        //        exception(ResType.ERROR);
        //                .set("${table.entityPath}s2", ${table.entityPath}s2)
        return SRS.bySuccess(${table.entityPath}s1);
    }

    @Override
    public SRS page(SRS param) {
        BaseWrapper<${entity}> wrapper = new BaseWrapper<>();
        wrapper.eq("1", 1);
        wrapper.orderByAsc(${entity}.CREATE_TIME);
        like(${entity}.ID, param.get("id"), wrapper);
        IPage<${entity}> ${table.entityPath}IPage1 =
        new ${entity}().selectPage(page(new Page<>(), param.getLong("current"), param.getLong("size")), wrapper);

        //IPage<${entity}> ${table.entityPath}IPage1 =
        //${table.entityPath}Dao.selectPage(Page(param.getLong("current"), param.getLong("size")), wrapper);
        //        MiPage ${table.entityPath}IPage2 =
        //                baseDao.selectPage(${entity}.TABLE_NAME, "*", param.getLong("current"), param.getLong("size"), wrapper);
        //                .set("${table.entityPath}IPage2", ${table.entityPath}IPage2)
        return SRS.bySuccess(${table.entityPath}IPage1);
    }

    @Override
    public SRS one(SRS param) {
        BaseWrapper<${entity}> wrapper = new BaseWrapper<>();
        eq(${entity}.ID, param.get("id"), wrapper);
        ${entity} ${table.entityPath}1 =
        ${table.entityPath}Dao.selectOne(wrapper);
        //        SRS ${table.entityPath}2 =
        //                baseDao.selectOne(wrapper);
        //                .set("${table.entityPath}2", ${table.entityPath}2)
        return SRS.bySuccess(${table.entityPath}1);
    }
}